<?php



/**
 * 分享
 */
class InCome extends Controller {

    public function __construct($ControllerName, $Action, $QueryString) {
        parent::__construct($ControllerName, $Action, $QueryString);
    }

   
   public function ajax_save(){
    $QueryDay = date('Y-m-d');
    $this->loadModel('mInCome');
    $this->loadModel('Statistics');
    $online_money = $_POST['online_money'];
    $offline_money = $_POST['offline_money'];
    $time = strtotime($_POST['time']);
    $total_money = $offline_money+ $this->Statistics->queryTotalMoneyByDate($QueryDay);
    $real_money  = $offline_money+ $this->Statistics->queryFactTotalMoneyByDate($QueryDay);
    $des = $_POST['des'];
    $id = $_POST['id'];
    if($id != ''){
          $data =  array(
                             'online_money' => $online_money,
                             'total_money' => $total_money,
                             'offline_money' =>$offline_money,
                             'real_money' =>$real_money,
                             'des' =>$des,
                             'add_time'=>$time
                          );
     $id  = $this->mInCome->updateInCome($id,$data);
    }else{
     $id  = $this->mInCome->createInCome($total_money,$offline_money,$online_money,$real_money,$des,$time);
   }
  
    echo $id;
  }
  

  
  public function ajax_Income_list(){
        $this->loadModel('mInCome');
        $pageSize = 10;
    	
    	if($_POST['pageSize']){
    		$pageSize = $_POST['pageSize'];
    	}
    	
    	//增加限制机制，防止每页取的数据过多
    	if($pageSize>10){
    		$pageSize = 10;
    	}
    	
    	$page = $_POST['page'];
    	if(!$page){
    		$page = 1;
    	}
    	$start =$_POST['start'];
    	$end = $_POST['end'];
    	$offset = ($page - 1) * $pageSize;
    	if($start){
    	   $where[] = 'add_time >= '.strtotime($start);
    	}
    	if($end){
    	   $where[] = 'add_time <= '.strtotime($end);
    	}
    		
    
    	
       $count_sql = 'select count(1) from income';
       if(count($where)>0){
    		$condition = implode(' and ',$where);
    		$count_sql = $count_sql.' where '.$condition;
    	}
    	error_log('condition='.$condition);
        $list = $this->mInCome->get_income_list($condition,'add_time desc',$offset,$pageSize);
        $total_amount = 0;
        $fact_total_amount = 0;
        foreach($list as $key => $val){
            
            $list[$key]['add_time'] = date("Y-m-d",$val['add_time']);
            $total_amount += $val['total_money'];
            $fact_total_amount += $val['real_money'];
        }
    	//获取所有的总数
    	$count = $this->Db->getOne($count_sql);
    	
    	$data = array(
    			'list' => $list,  //列表
    			'total' => $count,
    			'totalMoney'=>$total_amount,
    		    'fat_totalMoney'=>$fact_total_amount
    	);
    	//$this->echoJson($condition."=$start====".strtotime($start));
    	$this->echoJson($data);
  }

}
